from django import template
from django.db import models
import re

register = template.Library()
from django.templatetags.future import url

class Link(template.Node):
    def __init__(self, item):
        self.item = item

    def render(self, context):
        return self.item.render(context)

class ObjectLink(Link):
    def render(self, context):
        return self.item.resolve(context).get_absolute_url()

@register.tag(name='nurl')
def clink_tag(parser, token):
    bits = token.split_contents()
    if len(bits) < 2:
        raise TemplateSyntaxError("'%s' takes at least one argument"
                                  " (path to a view)" % bits[0])
    tp = parser.compile_filter(bits[1])
    p = re.compile("^'")
    if p.match(bits[1]):
        u = url(parser, token)
        return Link(u)
    else:
        return ObjectLink(tp)
